/*	This file is part of AstroAttack.
*
*	AstroAttack is free software; you can redistribute it and/or modify
*	it under the terms of the GNU General Public License as published by
*	the Free Software Foundation; either version 2 of the License, or
*	(at your option) any later version.
*	
*	AstroAttack is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*	GNU General Public License for more details.
*	
*	You should have received a copy of the GNU General Public License
*	along with AstroAttack; if not, write to the Free Software
*	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*	
*	$Id: StartupFrame.java 68 2007-01-06 06:10:59Z xdracox $
*/

package AstroAttack;

import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.SystemColor;
import java.awt.Insets;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.BorderFactory;
import javax.swing.UIManager;

import java.io.File;

import java.util.Vector;

public class StartupFrame extends JFrame implements ItemListener
{
	private static StartupFrame m_ref;
	
	
	public static StartupFrame getFrame()
	{
		if ( m_ref == null )
			m_ref = new StartupFrame();
		return m_ref;
	}
	
	private SettingsManager m_settings;
	
	private JComboBox m_worldsList;
	
	private JTextField m_frameRate;
	private JTextField m_width;
	private JTextField m_height;
	
	private JCheckBox m_fullscreen;
	private boolean m_enableFullscreen;
	
	private StartupFrame()
	{
		super("AstroAttack - Setup");
		
		// makes sure the settings manager gets initialized
		m_settings = SettingsManager.getManager();
		
		// setting the system look and feel should not throw an exception
		try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
		catch ( Exception e ) {}
		
		Container contents = getContentPane();
		
		contents.setBackground(SystemColor.control);
		contents.setLayout(new GridBagLayout());
		
		initGeneralPanel();
		initDisplayPanel();
		initButtons();
		
		pack();
		setResizable(false);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	private void initGeneralPanel()
	{
		// create the general options panel
		JPanel generalPanel = new JPanel();
		generalPanel.setLayout(new GridBagLayout());
		generalPanel.setBorder(BorderFactory.createTitledBorder("General"));
		
		// add the it to the content pane
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.fill = c.BOTH;
		c.insets = new Insets(5, 5, 5, 5);
		c.gridwidth = 3;
		getContentPane().add(generalPanel, c);
		
		File worldsDir = new File("data/worlds");
		Vector<String> worlds = new Vector<String>();
		
		for ( String w : worldsDir.list() )
		{
			if ( !w.startsWith(".") )
				worlds.add(w);
		}
		
		JLabel chooseWorldLabel = new JLabel("Choose world: ");
		m_worldsList = new JComboBox(worlds);
		
		c.gridwidth = 1;
		generalPanel.add(chooseWorldLabel, c);
		c.gridx++;
		generalPanel.add(m_worldsList, c);
	}
	
	private void initDisplayPanel()
	{
		JPanel displayPanel = new JPanel();
		displayPanel.setLayout(new GridBagLayout());
		displayPanel.setBorder(BorderFactory.createTitledBorder("Display"));
		
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 1;
		c.fill = c.BOTH;
		c.insets = new Insets(5, 5, 5, 5);
		c.gridwidth = 3;
		getContentPane().add(displayPanel, c);
		
		JLabel labels[] = new JLabel[3];
		labels[0] = new JLabel("Frame rate:");
		labels[1] = new JLabel("Width:");
		labels[2] = new JLabel("Height:");
				
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 1;
		c.anchor = c.LINE_START;
		
		for ( JLabel label : labels )
		{
			displayPanel.add(label, c);
			c.gridy++;
		}
		
		Settings s = m_settings.getAll();
		
		m_frameRate = new JTextField("" + s.display_framerate, 4);
		m_width = new JTextField("" + s.display_width, 4);
		m_height = new JTextField("" + s.display_height, 4);
		
		c.gridx = 1;
		c.gridy = 0;
		c.anchor = c.LINE_START;
		
		displayPanel.add(m_frameRate, c);
		c.gridy++;
		displayPanel.add(m_width, c);
		c.gridy++;
		displayPanel.add(m_height, c);
		
		m_enableFullscreen = s.display_fullscreen;
		m_fullscreen = new JCheckBox("Enable fullscreen", s.display_fullscreen);
		m_fullscreen.addItemListener(this);
		
		c.gridx = 0;
		c.gridy++;
		c.gridwidth = 2;
		
		displayPanel.add(m_fullscreen, c);
	}
	
	private void initButtons()
	{
		// JFrame.add() is forwarded to the content pane, so we're good
		
		JButton startButton = new JButton("Start");
		JButton saveButton = new JButton("Save");
		JButton exitButton = new JButton("Exit");
		
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 2;
		c.anchor = c.CENTER;
		
		add(startButton, c);
		c.gridx++;
		add(saveButton, c);
		c.gridx++;
		add(exitButton, c);
		
		startButton.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{
				// probably should save settings, so they take effect
				saveSettings();
				
				WorldManager wm = WorldManager.getManager();
				
				if ( m_settings.get("general", "world") == null )
				{
					System.err.println("x_x");
					System.exit(0);
				}
				
				setVisible(false);
				AstroAttack.start();
			}
		});
		
		saveButton.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{	// save button saves
				saveSettings();
			}
		});
		
		exitButton.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{	// exit button exits :)
				System.exit(0);
			}
		});
	}
	
	private void saveSettings()
	{
		Settings s = new Settings();
		
		s.general_world = (String)m_worldsList.getSelectedItem();
		
		s.display_framerate = Integer.parseInt(m_frameRate.getText());
		s.display_width = Integer.parseInt(m_width.getText());
		s.display_height = Integer.parseInt(m_height.getText());
		s.display_fullscreen = m_enableFullscreen;
		
		SettingsManager sm = SettingsManager.getManager();
		
		sm.setAll(s);
		sm.save();
	}
	
	public void itemStateChanged(ItemEvent e)
	{
		if ( e.getStateChange() == ItemEvent.SELECTED )
			m_enableFullscreen = true;
		else if ( e.getStateChange() == ItemEvent.DESELECTED )
			m_enableFullscreen = false;
	}
}
